<template>
  <div class="themeIntroducestyle">
    <!-- 标题 -->
    <h2>主题介绍</h2>
    <!-- 表单 -->
    <el-form label-width="100px" :model="datas" size="small" :rules="rules">
      <h5 style="color: #000; font-size: 14px">图片上传</h5>
      <!-- 上传主题介绍图 -->
      <div class="themeImg" @click="uploadI">
        <img v-if="!datas.imgUrl" draggable="false" src="../../../assets/images/imgs.png" alt="" />
        <img v-else draggable="false" :src="datas.imgUrl" alt="" />
        <p>上传主题介绍图</p>
      </div>
      <el-form-item label="图片宽度" prop="width" class="lef" :hide-required-asterisk="true">
        <el-input placeholder="请输入图片宽度" v-model.number="datas.width" type="number" maxlength="3" clearable>
          <template slot="append">px</template>
        </el-input>
      </el-form-item>
      <el-form-item label="图片高度" prop="height" class="lef" :hide-required-asterisk="true">
        <el-input placeholder="请输入图片高度" v-model.number="datas.height" type="number" maxlength="3" clearable>
          <template slot="append">px</template>
        </el-input>
      </el-form-item>
      <el-form-item label="宽高比例" class="lef">
        <el-input v-model="imgScale" :disabled="true" placeholder="" clearable maxlength="45"/>
      </el-form-item>
    </el-form>
    <!-- 上传图片 -->
    <uploadimg ref="upload" @uploadInformation="uploadInformation" />
  </div>
</template>
<script>
import uploadimg from "../../uploadImg"; // 图片上传
export default {
  name: "ThemeIntroducestyle",
  components: { 
    uploadimg 
  },
  props: {
    datas: Object,
  },
  data() {
    const checkWidth = (rule, value, callback) => {
      if (value < 0 || value === '') callback(new Error('此项必填，否则模块会显示异常'));
    }
    const checkHeight = (rule, value, callback) => {
      if (value < 0 || value === '') callback(new Error('此项必填，否则模块会显示异常'));
    }
    return {
      dragOptions: {
        animation: 200,
      },
      emptyText: "",
      rules: {
        width: [{ required: true, validator: checkWidth, trigger: "blur" }],
        height: [{ required: true, validator: checkHeight, trigger: "blur" }]
      }
    };
  },
  computed: {
    imgScale() {
      return this.datas.scale = (this.datas.width / this.datas.height).toFixed(2);
    },
  },
  methods: {
    uploadI() {
      this.$refs.upload.showUpload();
    },
    uploadInformation(res) {
      this.datas.imgUrl = res;
    }
  },
};
</script>
<style lang="scss" scoped>
.themeIntroducestyle{
  width: 100%;
  padding: 0 10px 20px;
  box-sizing: border-box;
  /* 标题 */
  h2 {
    padding: 24px 16px 24px 0;
    margin-bottom: 15px;
    border-bottom: 1px solid #f2f4f6;
    font-size: 18px;
    font-weight: 600;
    color: #323233;
  }
  /* 背景图 */
  .themeImg {
    display: inline-flex;
    align-items: center;
    cursor: pointer;
    width: 100px;
    height: 100px;
    position: relative;
    background: #f2f4f6;
    margin-bottom: 30px;
    img {
      width: 100%;
      height: auto;
    }
    p {
      position: absolute;
      left: 0;
      bottom: -10px;
      width: 100%;
      height: 20px;
      font-size: 12px;
      color: #fff;
      background: rgba(0, 0, 0, 0.5);
      text-align: center;
      line-height: 20px;
    }
  }

  .lef {
    .el-form-item__label {
      text-align: left;
    }
  }
}
</style>
<style>
  input::-webkit-outer-spin-button,
  input::-webkit-inner-spin-button {
    -webkit-appearance: none;
  }
  input[type="number"]{
    -moz-appearance: textfield;
    appearance: none;
  }
</style>